package com.direw.client;

import org.apache.catalina.connector.RequestFacade;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DireW
 */
@SpringBootApplication
@RestController
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

    @GetMapping("/trigger")
    public void trigger(ServletRequest servletRequest, ServletResponse servletResponse) {
        // 此处用java发起请求
        HttpURLConnection con;
        BufferedReader buffer;
        StringBuffer resultBuffer;
        try {
            System.out.println("创建URL");
            URL url = new URL("http://localhost:54321/test");
            //得到连接对象
            con = (HttpURLConnection) url.openConnection();
            //得到响应码
            int responseCode = con.getResponseCode();
            System.out.println("响应码：" + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                //得到响应流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                resultBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
                System.out.println("结果：" + resultBuffer.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
